﻿using UnityEditor;
using UnityEngine;
using UnityEngine.AI;


namespace UFrame
{
    public class PLShortcutKey : ScriptableObject
    {
        public const string KeyName = "Plateface/ShortKey/DisableSelectGameObect %#h";
        public const string KeyBox = "Plateface/2D/ResetBox2D %#B";
        public const string KeyPlayAnimation = "Plateface/ShortKey/PlayAnimation %#]";
        public const string KeyPlayAnimationFormat = "Plateface/ShortKey/SetAnimationFormat legacy";
        public const string KeyChange2DAnd3D = "Plateface/2D/Change2DAnd3D %#e";


        [MenuItem("Plateface/ShortKey/Log Path %#m", false)]
        static void LogSelectObjPath()
        {
            var go = Selection.objects;

            if (go == null || go.Length == 0)
            {
                return;
            }

            foreach (var item in go)
            {
                Debug.Log(UnityEditor.AssetDatabase.GetAssetPath(item));
            }
        }


        //根据当前有没有选中物体来判断可否用快捷键
        [MenuItem(KeyName, true)]
        static bool ValidateSelectEnableDisable()
        {
            GameObject[] go = GetSelectedGameObjects() as GameObject[];

            if (go == null || go.Length == 0)
            {
                return false;
            }

            return true;
        }

        [MenuItem(KeyName)]
        static void SeletEnable()
        {
            bool enable = false;
            GameObject[] gos = GetSelectedGameObjects() as GameObject[];

            foreach (GameObject go in gos)
            {
                enable = !go.activeInHierarchy;
                EnableGameObject(go, enable);
            }
        }

        [MenuItem(KeyBox)]
        static void ResetBox2D()
        {
            GameObject[] gos = GetSelectedGameObjects() as GameObject[];
            ResetBox2D(gos);
        }

        private static void ResetBox2D(GameObject[] gos)
        {
            foreach (GameObject go in gos)
            {
                var d2 = go.GetComponent<BoxCollider2D>();
                var d3 = go.GetComponent<BoxCollider>();
                var sr = go.GetComponent<SpriteRenderer>();
                var mr = go.GetComponent<MeshRenderer>();
                var nv = go.GetComponent<NavMeshObstacle>();

                if (d3 != null && mr != null)
                {
                    d3.center = Vector3.zero;
                    d3.size = Vector3.one;
                }
                else if (d2 != null && sr != null)
                {
                    d2.offset = Vector2.zero;

                    if (sr.drawMode == SpriteDrawMode.Sliced)
                    {
                        d2.size = sr.size;
                        if (nv != null)
                        {
                            nv.size = sr.size;
                        }
                    }

                    if (sr.drawMode == SpriteDrawMode.Simple)
                    {
                        d2.size = sr.bounds.size;
                        if (nv != null)
                        {
                            nv.size = sr.bounds.size;
                        }
                    }
                }
                else if (d3 != null && sr != null)
                {
                    Vector3 pos = Vector3.zero;
                    if (sr.drawMode == SpriteDrawMode.Sliced)
                    {
                        pos = new Vector3(sr.size.x, sr.size.y, 1);
                    }

                    if (sr.drawMode == SpriteDrawMode.Simple)
                    {
                        pos = new Vector3(sr.bounds.size.x, sr.bounds.size.y, 1);
                    }

                    d3.size = pos;
                    if (nv != null)
                    {
                        nv.size = pos;
                    }
                }
            }
        }

        [MenuItem(KeyChange2DAnd3D)]
        static void Change2DAnd3D()
        {
            GameObject[] gos = GetSelectedGameObjects();

            foreach (GameObject go in gos)
            {
                var box3D = go.GetComponent<BoxCollider>();
                var box2D = go.GetComponent<BoxCollider2D>();
                if (box2D != null)
                {
                    DestroyImmediate(box2D);
                    go.AddComponent<BoxCollider>();
                }
                else if (box3D != null)
                {
                    DestroyImmediate(box3D);
                    go.AddComponent<BoxCollider2D>();
                }
            }

            ResetBox2D(gos);
        }

        [MenuItem(KeyPlayAnimation)]
        static void PlayAnimation()
        {
            GameObject[] gos = GetSelectedGameObjects() as GameObject[];

            foreach (GameObject go in gos)
            {
                var d2 = go.GetComponent<Animation>();
                var d3 = go.GetComponent<Animator>();
                if (d2 != null)
                {
                    d2.Play();
                    Debug.Log("播放视频");
                }

                if (d3 != null)
                {
                    //var state = d3.GetCurrentAnimatorStateInfo(0);
                    d3.StopPlayback();
                }
            }
        }


        [MenuItem(KeyPlayAnimationFormat)]
        static void SetAnimationFormat()
        {
            foreach (AnimationClip go in Selection.objects)
            {
                go.legacy = !go.legacy;
            }
        }

        //获得选中的物体
        static GameObject[] GetSelectedGameObjects()
        {
            return Selection.gameObjects;
        }

        //激活或关闭当前选中物体
        public static void EnableGameObject(GameObject parent, bool enable)
        {
            parent.gameObject.SetActive(enable);
        }
    }
}